package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.AttendanceDeviceInfo;
import com.example.prison.service.AttendanceDeviceInfoService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "门禁设备管理")
@RestController
@RequestMapping("/api")
public class AttendanceDeviceInfoController {

    @Autowired
    private AttendanceDeviceInfoService attendanceDeviceInfoService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取门禁设备列表")
    @GetMapping("/getAttendanceDevice")
    public Response<PageInfo<AttendanceDeviceInfo>> getAttendanceDevice(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                        @Parameter(description = "设备编号") String deviceNumber,
                                                                        @Parameter(description = "设备名称") String deviceName) {
        PageInfo<AttendanceDeviceInfo> pageInfo = attendanceDeviceInfoService.get(pageNumber, pageSize, deviceNumber, deviceName);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增门禁设备")
    @PostMapping("/addAttendanceDevice")
    public Response<?> addAttendanceDevice(@RequestBody AttendanceDeviceInfo attDevice) {
        int result = attendanceDeviceInfoService.add(attDevice);
        if (result == -1) {
            return Response.error("设备已存在！");
        }
        log.info("新增门禁设备"+", 数据："+ JSON.toJSONString(attDevice));
//        systemLogService.addLog("新增报警设置");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除门禁设备")
    @PostMapping("/deleteAttendanceDevice")
    public Response<?> deleteAttendanceDevice(@Parameter(description = "设备ID") @RequestBody Integer[] ids) {
        attendanceDeviceInfoService.delete(ids);
        log.info("删除门禁设备");
//        systemLogService.addLog("删除门禁设备");
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑门禁设备")
    @PostMapping("/updateAttendanceDevice")
    public Response<?> updateAttendanceDevice(@RequestBody AttendanceDeviceInfo attDevice) {
        Integer id = attDevice.getDeviceId();
        int result = attendanceDeviceInfoService.update(attDevice, id);
        if (result == -1) {
            return Response.error("门禁设备编号已存在！");
        }
        log.info("编辑门禁设备"+", 数据："+JSON.toJSONString(attDevice));
//        systemLogService.addLog("编辑门禁设备");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取门禁设备信息")
    @GetMapping("/getAttendanceDeviceById")
    public Response<AttendanceDeviceInfo> getAttendanceDeviceById(@Parameter(description = "设备ID")@RequestParam Integer deviceId) {
        AttendanceDeviceInfo attendanceDevice = attendanceDeviceInfoService.getById(deviceId);
        if(ObjectUtils.isEmpty(attendanceDevice)) {
            return Response.error("此设备id不存在！");
        }
        return Response.success("获取成功！", attendanceDevice);
    }

//    @ApiOperation("依据设备名称获取门禁设备信息")
//    @GetMapping("/getAttendanceDeviceByName")
//    public Response<PageInfo<AttendanceDeviceInfo>> getAttendanceDeviceByName(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                        @Parameter(description = "设备名称")@RequestParam String deviceName) {
//        PageInfo<AttendanceDeviceInfo> pageInfo = attendanceDeviceInfoService.getByName(pageNumber, pageSize, deviceName);
//        if(ObjectUtils.isEmpty(pageInfo.getList())) {
//            return Response.error("此门禁设备不存在！");
//        }
//        return Response.success("获取成功！", pageInfo);
//    }
//
//    @ApiOperation("依据设备编号获取门禁设备信息")
//    @GetMapping("/getAttendanceDeviceByNumber")
//    public Response<PageInfo<AttendanceDeviceInfo>> getAttendanceDeviceByNumber(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                        @Parameter(description = "设备编号")@RequestParam String deviceNumber) {
//        PageInfo<AttendanceDeviceInfo> pageInfo = attendanceDeviceInfoService.getByNumber(pageNumber, pageSize, deviceNumber);
//        if(ObjectUtils.isEmpty(pageInfo.getList())) {
//            return Response.error("此门禁设备不存在！");
//        }
//        return Response.success("获取成功！", pageInfo);
//    }

    @ApiOperation("获取门禁设备实时状态")
    @GetMapping("/getAttendanceDeviceStatus")
    public Response<PageInfo<AttendanceDeviceInfo>> getAttendanceDeviceStatus(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<AttendanceDeviceInfo> pageInfo = attendanceDeviceInfoService.getRealStatus(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

}
